package org.summerframework.component.security;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.summerframework.component.security.enumeration.LoginAccountEnum;
import org.summerframework.component.security.enumeration.LoginFromEnum;
import org.summerframework.component.security.enumeration.LoginSecurityEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包装了一些登录参数
 *
 * @author 石超
 */
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({"name", "login", "sessionExpiringTime", "json", "user"})
public class LoginData<T> implements Serializable, Principal {
    private static final long serialVersionUID = -2887948530307269951L;

    private String email;
    private String mobile;
    private String userName;
    private String nickname;
    private String realName;
    private String avatar;
    private String password;
    private String redirectUrl;
    private String loginIp;
    private String userAgent;
    private String deviceToken;
    private Long userId;
    private T user;
    private Long identity;
    private Integer sex;
    private String sexName;
    private LoginAccountEnum loginAccountEnum;
    private LoginFromEnum loginFromEnum;
    private Integer loginType;
    private Integer source;
    private String sourceStr;
    private LoginSecurityEnum loginSecurityEnum = LoginSecurityEnum.HTTPS;
    private String callback;
    private String style = "default";
    private Long loginLogId;
    private Map<String, Object> result = new HashMap<>();
    private int json = 0;
    private int sessionExpiringTime = Constants.SESSION_EXPIRING_TIME;
    private Boolean isNotInitPassword = false;
    private int failCount = 0;
    private int status = 0;
    private Integer userType;
    private String sessionId;
    private String sessionKey;
    private List<Long> roleIds;

    /**
     * 是否只能唯一登录
     */
    private boolean onlyUser;
    private Map<String, Object> param = new HashMap<>();

    public Map<String, Object> getParam() {
        return param;
    }

    public <O> O getParam(String key) {
        return (O) param.get(key);
    }

    public Object putParam(String key, Object value) {
        return param.put(key, value);
    }

    public void setSessionExpiringTime(int sessionExpiringTime) {
        if (sessionExpiringTime != 0) {
            this.sessionExpiringTime = sessionExpiringTime;
        }
    }

    public void clearResult() {
        result.clear();
    }

    public void putResult(Map<String, Object> map) {
        if (map != null) {
            result.putAll(map);
        }
    }

    public void putResult(String key, Object value) {
        result.put(key, value);
    }

    public <T> T getResult(String key) {
        return (T) result.get(key);
    }

    public boolean isLogin() {
        return getUser() != null;
    }

    public boolean isJson() {
        return json != 0;
    }

    @Override
    public String getName() {
        if (this.userId != null) {
            return String.valueOf(this.userId);
        }
        return "0";
    }
}
